package com.kylin.module.admin.action;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.Order;

import com.kylin.common.Constants;
import com.kylin.common.dict.orm.Dict;
import com.kylin.common.entity.MsgKey;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;
import com.kylin.common.util.MemObjFactory;
import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.PropertyCondition;
import com.kylin.module.admin.business.ManageShopBlogic;
import com.kylin.module.shop.orm.Company;
import com.kylin.module.user.orm.User;
import com.kylin.struts.BaseActionInterface;
import com.kylin.struts.BaseDispatchAction;

/**
 * 后台管理接口：管理商城与公司
 * @author lzh
 *
 */
public class ManageShopAction  extends BaseDispatchAction {

	protected static Logger log = Logger.getLogger(ManageShopAction.class);


	/**
	 * 查询返回待审核公司列表
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward searchRegCompany(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("查询待审核公司 : searchId="+request.getParameter("searchId"));
		String forward = "listRegCompany";
		int pageNum = 1;
		String searchId = request.getParameter("searchId");
		CriteriaInfo cinfo = null;
		try {
			if (request.getParameter("page") != null) {
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if (searchId == null) {
				PropertyCondition[] pConditions = new PropertyCondition[] {
						new PropertyCondition("userrole", Constants.USER_ROLE_COMPANY,PropertyCondition.OPERATOR_EQ ),
						new PropertyCondition("active", Constants.ACTIVE_WAIT,PropertyCondition.OPERATOR_EQ )
				};				
				cinfo = new CriteriaInfo(User.class, pConditions, Order.desc("createdon"));
				addUserCacheCriteriaInfo(request, cinfo);
			}else{
				cinfo = getUserCacheCriteriaInfo(request, searchId);
			}
			ManageShopBlogic bean = new ManageShopBlogic();
			DisplayPage page = bean.getCompanyUserList(pageNum, cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
		}catch (Exception e) {
			doException( request, e, "listRegCompany");
		}
		return mapping.findForward(forward);
	}
	
	/**
	 * 查询已审核通过的公司列表
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward searchCompany(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("查询审核通过公司 : searchId="+request.getParameter("searchId"));
		String forward = "listCompany";
		int pageSize = 10;
		int pageNum = 1;
		String searchId = request.getParameter("searchId");
		CriteriaInfo cinfo = null;
		try {
			if (request.getParameter("page") != null) {
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if (searchId == null) {
				List condsList = new LinkedList();
				String language = request.getParameter("language");
				if(language==null)
					language = getSessionLocale(request).toString();
				condsList.add(new PropertyCondition("language",language,PropertyCondition.OPERATOR_EQ ));
				condsList.add(new PropertyCondition("user.active", new Short[]{Constants.ACTIVE_ON,Constants.ACTIVE_OFF},PropertyCondition.OPERATOR_IN ));
				if( hasParam(request.getParameter("name")) ){
					String likeExp = "%"+request.getParameter("name")+"%";
					condsList.add(new PropertyCondition("name",likeExp,PropertyCondition.OPERATOR_LIKE));
				}
				 
				if( hasParam(request.getParameter("userName")) ){
					String userName = request.getParameter("userName");
					condsList.add(new PropertyCondition("user.username",userName,PropertyCondition.OPERATOR_EQ));
				}
				//按行业类别或产品类别，涉及set集合
//				if( hasParam(request.getParameter("mainbusiness")) ){
//					Long mainbusinessid = new Long(request.getParameter("mainbusiness"));
//					condsList.add(new PropertyCondition("user.country",country,PropertyCondition.OPERATOR_EQ));
//				}
			    if( hasParam(request.getParameter("language")))
				    condsList.add(new PropertyCondition("language",request.getParameter("language"),PropertyCondition.OPERATOR_EQ ));
				
				PropertyCondition[] array = (PropertyCondition[])condsList.toArray(new PropertyCondition[0]);
				cinfo = new CriteriaInfo(Company.class, array, Order.desc("createdon"));//原本是按最早排在最前，后应要求改为最新排最前 lzh 20080610
				addUserCacheCriteriaInfo(request, cinfo);
			}else{
				cinfo = getUserCacheCriteriaInfo(request, searchId);
			}
			DisplayPage page =  PageManager.getDisplayPage(pageSize, pageNum, cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
		}catch (Exception e) {
			doException( request, e, "listCompany");
		}
		return mapping.findForward(forward);
	}

	/**
	 * 显示公司注册信息
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward regCompanyDetail(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("显示公司注册信息 : ");
		String forward = "regCompanyDetail";
		try {
			Long id = new Long(request.getParameter("userid"));
			ManageShopBlogic bean = new ManageShopBlogic();
			List list = bean.getUserCompanys(id);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, list);
		}catch (Exception e) {
			doException(request, e, "regCompanyDetail");
		}
		return mapping.findForward(forward);
	}

	/**
	 * 显示审核过的公司的详细信息
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward companyDetail(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("显示审核过公司的详细信息 : "+request.getParameter("userid"));
		String forward = "companyDetail";
		try {
			ManageShopBlogic bean = new ManageShopBlogic();
			Long id = new Long(request.getParameter("userid"));
			List list = bean.getUserCompanys(id);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, list);
		}catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 改变公司帐户状态
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward changeCompanyActive(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("改变公司帐户状态 : "+request.getParameter("userid"));
		String forward = "companyDetail";
		try {
			ManageShopBlogic bean = new ManageShopBlogic();
			Long id = new Long(request.getParameter("userid"));
			Short active = new Short(request.getParameter("active"));
			bean.setCompanyActive(id,active);
			List list = bean.getUserCompanys(id);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, list);
			request.setAttribute(Constants.MSGKEY, new MsgKey("","改变帐户状态成功"));;
		}catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 激活用户
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward activationCompany(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		String forward = "activationResult";
		try {
			Long id = new Long(request.getParameter("userid"));
			ManageShopBlogic bean = new ManageShopBlogic();
			String mailContent = bean.activationRegCompany(id);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, "已经激活此用户,发送通知邮件如下<hr><br> "+mailContent);
		}catch (Exception e) {
			doException(request, e,forward);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, "激活此用户失败");
		}
		return mapping.findForward(forward);
	}
	
	/**
	 * 查找指定一级分类的所属二级产品分类列表
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward selectSubProductCat(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		String  forward = "listProductDict";
		List rs = new ArrayList();
		String parentId = request.getParameter("parent");
		if("".equals(parentId))
			parentId = null;
		String language = request.getParameter("language");
	    List catProduct = MemObjFactory.getDict("产品类别");
	    if(catProduct != null){
	    	for(int i=0;i<catProduct.size();i++){
	    		Dict dict = (Dict)catProduct.get(i);
	    		if(dict.getLanguage() != null && dict.getLanguage().equals(language))
	    			rs.add(dict);
	    	}
	    }
	    request.setAttribute(Constants.KEY_REQUEST_RESULT, rs);
		return mapping.findForward(forward);
	}

	public ActionForward changeParent(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		String  forward = "listProductDict";
		String [] ids = request.getParameterValues("ids");
		String parentId = request.getParameter("parent");
		String language = request.getParameter("language");
		try {
			ManageShopBlogic bean = new ManageShopBlogic();
			bean.changeProductCatsParent(ids, parentId,language);
		}catch (Exception e) {
			doException(request, e,forward);
		}
		return mapping.findForward(forward);
	}

	public ActionForward update(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

	public ActionForward add(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

}
